package com.azxcrm.project.system.customer.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.azxcrm.common.utils.security.ShiroUtils;
import com.azxcrm.framework.web.controller.BaseController;
import com.azxcrm.framework.web.domain.Message;
import com.azxcrm.framework.web.page.TableDataInfo;
import com.azxcrm.project.system.customer.domain.CustomerAttendance;
import com.azxcrm.project.system.customer.service.ICustomerAttendanceService;
import com.azxcrm.project.system.dept.domain.Dept;
import com.azxcrm.project.system.dept.service.IDeptService;
import com.azxcrm.project.system.user.domain.User;
import com.azxcrm.project.system.user.service.IUserService;
import com.azxcrm.project.system.wap.domain.CustomerVisitRecord;
import com.azxcrm.project.system.wap.service.ICustomerVisitRecordService;

/**
 * 客户参课列 控制层处理
 * 
 * @author Dustyone
 * @date 2018-08-28
 */
@Controller
@RequestMapping("/module/customerAttendance")
public class CustomerAttendanceController extends BaseController
{
    private String prefix = "module/customerAttendance";
	
    /**
	 * 业务员角色
	 */
	@Value("${crm.role.salesman}")
	private String salesman;

	/**
	 * 管理员
	 */
	@Value("${crm.role.admin}")
	private String admin;
	
	@Value("${crm.role.departmentManager}")
	private String departmentManager;
    
	@Autowired
	private ICustomerAttendanceService customerAttendanceService;
	
	@Autowired
	private IDeptService iDeptService;
	
	@Autowired
	private IUserService userService;
	
	@Autowired
	private ICustomerVisitRecordService customerVisitRecordService;
	
	@GetMapping()
	@RequiresPermissions("module:customerAttendance:view")
	String customerAttendance()
	{
	    return prefix + "/customerAttendance";
	}
	
	/**
	 * 查询客户参课列列表
	 */
	@GetMapping("/list")
	@RequiresPermissions("module:customerAttendance:list")
	@ResponseBody
	public TableDataInfo list(CustomerAttendance customerAttendance)
	{
		setPageInfo(customerAttendance);
		if(ShiroUtils.hasRole(salesman)){
			customerAttendance.setSalesUserId(ShiroUtils.getUserId().intValue());
		}
        List<CustomerAttendance> list = customerAttendanceService.selectCustomerAttendanceList(customerAttendance);
		return getDataTable(list);
	}
	
	/**
	 * 新增客户参课列
	 */
	@GetMapping("/add")
	@RequiresPermissions("module:customerAttendance:add")
	public String add(User user, Dept dept, Model model)
	{	
		List<User> salesList = new ArrayList<>();
		
		//部门经理查询当前部门下所有业务员信息
		if(ShiroUtils.hasRole("departmentManager")){
			user = ShiroUtils.getUser();
			dept = iDeptService.selectDeptById(user.getDeptId());
			salesList = userService.selectUserByDeptId(dept.getDeptId());
			model.addAttribute("salesList", salesList);
		}
		
		//业务员查询当前自己的信息
		if(ShiroUtils.hasRole(salesman)){
			user = ShiroUtils.getUser();
			dept = iDeptService.selectDeptById(user.getDeptId());
			//
			salesList = userService.selectUserList(user);
			model.addAttribute("salesList", salesList);
		}
		model.addAttribute("dept", dept);
		model.addAttribute("salesList", salesList);
		
	    return prefix + "/add";
	}

	/**
	 * 修改客户参课列
	 */
	@GetMapping("/edit/{attendId}")
	@RequiresPermissions("module:customerAttendance:edit")
	public String edit(@PathVariable("attendId") Integer attendId, Model model)
	{
		CustomerAttendance customerAttendance = customerAttendanceService.selectCustomerAttendanceById(attendId);
		CustomerVisitRecord customerVisitRecord = customerVisitRecordService.selectCustomerVisitRecordById(customerAttendance.getSalesUserId());
		model.addAttribute("customerAttendance", customerAttendance);
		model.addAttribute("customerVisitRecord", customerVisitRecord);
	    return prefix + "/edit";
	}
	
	/**
	 * 保存客户参课列
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("module:customerAttendance:add")
	public Message save(CustomerAttendance customerAttendance)
	{
		if (customerAttendanceService.saveCustomerAttendance(customerAttendance) > 0)
		{
			return Message.ok();
		}
		return Message.error();
	}
	
	/**
	 * 删除客户参课列
	 */
	@PostMapping( "/remove/{attendId}")
	@ResponseBody
	@RequiresPermissions("module:customerAttendance:remove")
	public Message remove(@PathVariable("attendId") Integer attendId)
	{
		if (customerAttendanceService.deleteCustomerAttendanceById(attendId) > 0)
		{
		    return Message.ok();
		}
		return Message.error();
	}
	
	/**
	 * 批量删除客户参课列
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("module:customerAttendance:batchRemove")
	public Message remove(@RequestParam("ids[]") Integer[] attendIds)
	{
		int rows = customerAttendanceService.batchDeleteCustomerAttendance(attendIds);
		if (rows > 0)
        {
            return Message.ok();
        }
        return Message.error();
	}
	
}
